package com.gwnet.shop.controller;

import java.util.List;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.gwnet.common.core.domain.ResponseResult;
import com.gwnet.common.log.annotation.Log;
import com.gwnet.common.log.enums.BusinessType;
import com.gwnet.common.mybatis.core.page.TableDataInfo;
import com.gwnet.common.satoken.utils.LoginHelper;
import com.gwnet.shop.domain.bo.OrderInfoIdBo;
import com.gwnet.shop.utils.AjaxResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gwnet.shop.domain.OrderInfo;
import com.gwnet.shop.service.IOrderInfoService;
import javax.annotation.Resource;

/**
 * 订单Controller
 * @author 张万鹏
 * @date 2024-07-29
 */
@RestController
@RequestMapping("/orderinfo")
public class OrderInfoController extends MBaseController
{
    @Resource
    private IOrderInfoService orderInfoService;

    /**
     * 查询订单列表
     */
    @SaCheckPermission("shop:orderinfo:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo)
    {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return getDataTable(list);
    }

    /**
     * 查询订单列表 及 对应购买的商品
     */
    @SaCheckPermission("shop:orderinfo:list")
    @GetMapping("/list/detail")
    public TableDataInfo listDetail(OrderInfo orderInfo)
    {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoListDetail(orderInfo);
        return getDataTable(list);
    }


    /**
     * 获取订单详细信息
     */
    @SaCheckPermission("shop:orderinfo:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(orderInfoService.selectOrderInfoByOrderId(orderId));
    }

    /**
     * 获取订单详细信息
     */
    @SaCheckPermission("shop:orderinfo:query")
    @GetMapping(value = "/detail/{orderId}")
    public AjaxResult getInfoDetail(@PathVariable("orderId") String orderId)
    {
        return success(orderInfoService.selectOrderInfoDetailByOrderId(orderId));
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("shop:orderinfo:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }


    /**
     * 删除订单
     */
    @SaCheckPermission("shop:orderinfo:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(orderInfoService.deleteOrderInfoByOrderIds(orderIds));
    }

    /**
     * 用户主动触发订单结算，例如确认收货
     * @param orderInfo
     * @return
     */
    @PostMapping("/orderCommissionSettlement")
    public ResponseResult orderCommissionSettlement(@RequestBody OrderInfoIdBo orderInfo){
        OrderInfo order = orderInfoService.getById(orderInfo.getOrderId());
        orderInfoService.orderCommissionSettlement(List.of(order));
        return ResponseResult.success();
    }


}
